package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private  NursingProjectMapper nursingProjectMapper;
    @Override
    public PageResponse<NursingProjectVo> selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //构建分页插件
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> nursingProjects = nursingProjectMapper.selectByPage(name, status, pageNum, pageSize);
        return PageResponse.of(nursingProjects, NursingProjectVo.class);
    }

    @Override
    public void insert(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo selectById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setUpdateBy(1L);
//        nursingProject.setUpdater(nursingProjectDto.get);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void startOrStop(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id, status);
    }

    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProjectVo> findAll() {
        List<NursingProjectVo> nursingProjectVos = nursingProjectMapper.listAll();
        return nursingProjectVos;
    }
}
